package keshe;

import keshe.dayin.Test;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;
//主要功能界面
public class Function extends JFrame implements ActionListener {
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p6 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    JPanel p5 = new JPanel();
    JButton add = new JButton("增加学生");
    JButton del = new JButton("删除学生");
    JButton update = new JButton("修改学生");
    JButton single = new JButton("单个查询");
    JButton look = new JButton("查看全体");
    JButton b11 = new JButton("按年级查找");
    JButton b12 = new JButton("按班别查找");
    JButton b13 = new JButton("按专业查找");
    JLabel la1 = new JLabel("学生管理");
    JLabel la2 = new JLabel("成绩管理");
    JLabel la3 = new JLabel("学生查找");
    JButton b1 = new JButton("单科排名");
    JButton b2 = new JButton("打印成绩");
    JButton b3 = new JButton("合格学生");
    JButton b4 = new JButton("挂科学生");
    JMenuItem items[][] = new JMenuItem[4][];
    JMenuBar bar = new JMenuBar();
    JMenu m1 = new JMenu("开始");
    JMenu m2 = new JMenu("学生管理");
    JMenu m3 = new JMenu("学生查找");
    JMenu m4 = new JMenu("成绩管理");


    //构造方法
    public Function() {
        setTitle("学生成绩管理系统");
        //设计菜单栏
        items[0] = new JMenuItem[2];
        items[1] = new JMenuItem[4];
        items[2] = new JMenuItem[4];
        items[3] = new JMenuItem[4];
        m1.add(items[0][0] = new JMenuItem("返回"));
        m1.add(items[0][1] = new JMenuItem("退出"));
        m2.add(items[1][0] = new JMenuItem("增加学生"));
        m2.add(items[1][1] = new JMenuItem("删除学生"));
        m2.add(items[1][2] = new JMenuItem("修改学生"));
        m2.add(items[1][3] = new JMenuItem("单个查询"));
        m3.add(items[2][0] = new JMenuItem("查看全体"));
        m3.add(items[2][1] = new JMenuItem("按年级查找"));
        m3.add(items[2][2] = new JMenuItem("按班别查找"));
        m3.add(items[2][3] = new JMenuItem("按专业查找"));
        m4.add(items[3][0] = new JMenuItem("单科排名"));
        m4.add(items[3][1] = new JMenuItem("打印成绩"));
        m4.add(items[3][2] = new JMenuItem("合格学生"));
        m4.add(items[3][3] = new JMenuItem("挂科学生"));
        bar.add(m1);
        bar.add(m2);
        bar.add(m3);
        bar.add(m4);


        //设计布局
        getContentPane().setLayout(new GridLayout(7, 1));
        getContentPane().add(bar);
        getContentPane().add(p1);
        getContentPane().add(p2);
        getContentPane().add(p6);
        getContentPane().add(p3);
        getContentPane().add(p4);
        getContentPane().add(p5);
        p1.add(la1);
        p2.add(add);
        p2.add(del);
        p2.add(update);
        p2.add(single);
        p6.add(la3);
        p3.add(look);
        p3.add(b11);
        p3.add(b12);
        p3.add(b13);
        p4.add(la2);
        p5.add(b1);
        p5.add(b2);
        p5.add(b3);
        p5.add(b4);

        setBounds(500, 250, 100, 100);
        pack();
        setVisible(true);

        //注册监听器
        add.addActionListener(this);
        del.addActionListener(this);
        update.addActionListener(this);
        look.addActionListener(this);
        single.addActionListener(this);
        b11.addActionListener(this);
        b12.addActionListener(this);
        b13.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);

        items[0][0].addActionListener(this);
        items[0][1].addActionListener(this);
        items[1][0].addActionListener(this);
        items[1][1].addActionListener(this);
        items[1][2].addActionListener(this);
        items[1][3].addActionListener(this);
        items[2][0].addActionListener(this);
        items[2][1].addActionListener(this);
        items[2][2].addActionListener(this);
        items[2][3].addActionListener(this);
        items[3][0].addActionListener(this);
        items[3][1].addActionListener(this);
        items[3][2].addActionListener(this);
        items[3][3].addActionListener(this);
    }

    //按钮事件响应，对用对应的类对象
    public void actionPerformed(@NotNull ActionEvent e) {

        if (e.getActionCommand() == "增加学生") {
            AddStudent addStudent = new AddStudent();

        } else if (e.getActionCommand() == "删除学生") {
            DeleteStudent delStudent = new DeleteStudent();
        } else if (e.getActionCommand() == "修改学生") {
            UpdateStudent delStudent = new UpdateStudent();
        } else if (e.getActionCommand() == "单个查询") {
            IndexSingle is = new IndexSingle();
        } else if (e.getActionCommand() == "查看全体") {
            LookAllStudents look = new LookAllStudents();
        } else if (e.getActionCommand() == "按年级查找") {
            IndexOfGrade ig = new IndexOfGrade();
        } else if (e.getActionCommand() == "按班别查找") {
            IndexOfClass ic = new IndexOfClass();
        } else if (e.getActionCommand() == "按专业查找") {
            IndexOfMajor im = new IndexOfMajor();
        } else if (e.getActionCommand() == "单科排名") {
            SubjectScoreRanking subject = new SubjectScoreRanking();
        }
        else if(e.getActionCommand()=="打印成绩"){
            Test test=new Test();//Poi save=new poi(text.getText());
           //TotalScoreRanking subject= new TotalScoreRanking();
        }
        else if(e.getActionCommand()=="合格学生"){
            PassStudents pass=new PassStudents();
        }
        else if(e.getActionCommand()=="挂科学生"){
            NotPassStudents notPass=new NotPassStudents();
        }
        else if(e.getActionCommand()=="返回"){
            setVisible(false);
            Login login=new Login();
            login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        else if(e.getActionCommand()=="退出"){
            System.exit(0);
        }
    }

    /**
     * 1.简单的JDBC工具，将数据库的配置及常用操作getConnection与closeConnection封装起来 为使用者访问数据库提供一个统一的入口
     * 2.main函数中是使用该工具的范例
     * 3.该类主要用于教学，实际使用中应该使用数据库连接池提高性能
     * 4.该工具需要JDBC 4.0以上的JDBC API
     * 5.可改进:driverName,url,userName,password这些可以放入配置文件，然后动态读取.
     * 6.该类仅用于访问MySQL数据库，main方法的范例中使用到的表名为user,有两列int id,string name
     *
     *
     */
    public static class JDBCUtil {

        private static final String DRIVERNAME = "com.mysql.jdbc.Driver";
        private static final String URL = "jdbc:mysql://localhost:3306/fjx？characterEncoding=utf8";
        private static final String USERNAME = "root";
        private static final String PASSWORD = "root";

        public static void registerDriver() {
            try {
                Class.forName(DRIVERNAME);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("找不到驱动");
            }
        }

        /**
         * 连接数据库
         * @return
         * @throws SQLException
         */
        public static Connection getConnection() throws SQLException {
            Connection con = null;
            //System.out.println("正在连接数据库...");
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            //System.out.println("数据库已连接!");
            return con;
        }

        /**
         * 有时仅需要关闭Connection，就可以调用此方法
         * @param con
         */
        public static void closeConnection(Connection con) {
            //System.out.println("正在释放所有资源...");
            if (con != null) {
                try {
                    con.close();
                    con = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

        /*
         * 释放所有资源
         */
        public static void realeaseAll(ResultSet rs, Statement st, Connection conn) {
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (st != null) {
                try {
                    st.close();
                    st = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            closeConnection(conn);
        }

    }
}

